package top.anka1.fxgui;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import top.anka1.ims.*;
import top.anka1.ims.RestaurantManagementSystem.Ingredient;

import java.util.ArrayList;
/**
 * Controller class for the Remove Ingredients GUI.
 * This class handles the display and removal of ingredients from the restaurant's inventory.
 * @author Anka
 */
public class RemoveIngredientsController {
    private RestaurantGUI restaurantGUI;
    @FXML
    private TableView<Ingredient> ingredientsTable;
    @FXML
    private TableColumn<Ingredient, String> nameColumn;
    @FXML
    private TableColumn<Ingredient, Double> priceColumn;
    public void setData(RestaurantGUI restaurantGUI) {
        this.restaurantGUI = restaurantGUI;
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        nameColumn.setStyle("-fx-alignment: CENTER;");
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
        priceColumn.setStyle("-fx-alignment: CENTER;");

        priceColumn.setCellFactory(column -> new javafx.scene.control.cell.TextFieldTableCell<>(
                new javafx.util.converter.DoubleStringConverter() {
                    @Override
                    public String toString(Double value) {
                        if (value == null) return "";
                        return String.format("¥%.2f", value);
                    }
                }
        ));

        ArrayList<Ingredient> ingredientList = new ArrayList<>();
        for (String name : restaurantGUI.getRestaurantService().getIngredientInventory().keySet()) {
            Ingredient ingredient = restaurantGUI.getRestaurantService().getIngredientInventory().get(name);
            ingredientList.add(ingredient);
        }
        ObservableList<Ingredient> ingredients = FXCollections.observableArrayList(ingredientList);
        ingredientsTable.setItems(ingredients);
    }
    public void showError(Stage stage, String title, String HeaderText, String contentText) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.initOwner(stage);
        alert.setTitle(title);
        alert.setHeaderText(HeaderText);
        alert.setContentText(contentText);
        alert.showAndWait();
    }

    @FXML
    public void confirmRemove() {
        Ingredient selectedIngredient = ingredientsTable.getSelectionModel().getSelectedItem();
        if (selectedIngredient != null) {
            try {
                restaurantGUI.getRestaurantService().getIngredientInventory().remove(selectedIngredient.getName());
                ingredientsTable.getItems().remove(selectedIngredient);
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Success");
                alert.setHeaderText("Ingredient Removed");
                alert.setContentText("Ingredient " + selectedIngredient.getName() + " has been removed successfully.");
                alert.showAndWait();
            } catch (Exception e) {
                showError((Stage) ingredientsTable.getScene().getWindow(), "Error", "Failed to remove ingredient", "Error: " + e.getMessage());
            }
        } else {
            // Handle case where no ingredient is selected
            showError((Stage) ingredientsTable.getScene().getWindow(), "Error", "No Ingredient Selected", "Please select an ingredient to remove.");
        }
    }
}
